private static final int REQUEST_READ_STORAGE = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 检查并请求存储权限
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) 
        != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, 
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
            REQUEST_READ_STORAGE);
    } else {
        // 权限已授予，执行扫描媒体文件逻辑
        scanMediaFiles();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 
                                     @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_READ_STORAGE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // 权限被授予，执行扫描媒体文件逻辑
            scanMediaFiles();
        } else {
            // 权限被拒绝，提示用户
            Toast.makeText(this, "需要存储权限才能访问媒体文件", Toast.LENGTH_SHORT).show();
        }
    }
}

private void scanMediaFiles() {
    // 扫描媒体文件的逻辑，调用 MediaManager 或其他相关类
    MediaManager mediaManager = new MediaManager();
    List<MediaItem> mediaItems = mediaManager.scanMedia();
    // 更新 UI 显示缩略图等
}